<?php
/*
 * Copyright (c) 2006 Byrne Reese. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the BSD License.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *
 * @author Byrne Reese <byrne@majordojo.com>
 * @version 0.2
 */

require_once("lib/config.php");
require_once("examples/ExampleGoogleCheckoutServer.php");
connect_to_db();
$server = new ExampleGoogleCheckoutServer();

$ordernum = "270284924743930"; 

$post1 = <<<END
<form action="simulate-post.php" method="post">
<input type="hidden" name="_type" value="new-order-notification" />
<input type="hidden" name="timestamp" value="2008-10-26T21:37:53.919Z" />
<input type="hidden" name="shopping-cart_items_item-1_item-name" value="Test Run" />
<input type="hidden" name="shopping-cart_items_item-1_item-description" value="Single Site License" />
<input type="hidden" name="shopping-cart_items_item-1_quantity" value="1" />
<input type="hidden" name="shopping-cart_items_item-1_unit-price" value="750.0" />
<input type="hidden" name="shopping-cart_items_item-1_unit-price_currency" value="USD" />
<input type="hidden" name="shopping-cart_items" value="shopping-cart.items.item-1" />
<input type="hidden" name="order-adjustment_total-tax_currency" value="USD" />
<input type="hidden" name="order-adjustment_total-tax" value="0.0" />
<input type="hidden" name="order-adjustment_adjustment-total_currency" value="USD" />
<input type="hidden" name="order-adjustment_adjustment-total" value="0.0" />
<input type="hidden" name="buyer-id" value="138434689571059" />
<input type="hidden" name="google-order-number" value="$ordernum" />
<input type="hidden" name="buyer-shipping-address_email" value="byrnereese@gmail.com" />
<input type="hidden" name="buyer-shipping-address_contact-name" value="Byrne Reese" />
<input type="hidden" name="buyer-shipping-address_address1" value="755 Walker Ave" />
<input type="hidden" name="buyer-shipping-address_country-code" value="US" />
<input type="hidden" name="buyer-shipping-address_city" value="oakland" />
<input type="hidden" name="buyer-shipping-address_region" value="CA" />
<input type="hidden" name="buyer-shipping-address_postal-code" value="94610" />
<input type="hidden" name="buyer-billing-address_email" value="byrnereese@gmail.com" />
<input type="hidden" name="buyer-billing-address_contact-name" value="Byrne Reese" />
<input type="hidden" name="buyer-billing-address_address1" value="755 Walker Ave" />
<input type="hidden" name="buyer-billing-address_country-code" value="US" />
<input type="hidden" name="buyer-billing-address_city" value="oakland" />
<input type="hidden" name="buyer-billing-address_region" value="CA" />
<input type="hidden" name="buyer-billing-address_postal-code" value="94610" />
<input type="hidden" name="buyer-marketing-preferences_email-allowed" value="true" />
<input type="hidden" name="order-total" value="750.0" />
<input type="hidden" name="order-total_currency" value="USD" />
<input type="hidden" name="fulfillment-order-state" value="NEW" />
<input type="hidden" name="financial-order-state" value="REVIEWING" />
<input type="hidden" name="serial-number" value="$ordernum-00001-7" />
<input type="submit" value="New Order Notification" />
</form>
END;

$post2 = <<<END
<form action="simulate-post.php" method="post">
<input type="hidden" name="_type" value="order-state-change-notification" />
<input type="hidden" name="timestamp" value="2008-10-26T21:39:05.011Z" />
<input type="hidden" name="serial-number" value="$ordernum-00005-1" />
<input type="hidden" name="google-order-number" value="$ordernum" />
<input type="hidden" name="new-fulfillment-order-state" value="NEW" />
<input type="hidden" name="new-financial-order-state" value="CHARGEABLE" />
<input type="hidden" name="previous-fulfillment-order-state" value="NEW" />
<input type="hidden" name="previous-financial-order-state" value="REVIEWING" />
<input type="submit" value="Order State Change Notification" />
</form>
END;

$post3 = <<<END
<form action="simulate-post.php" method="post">
<input type="hidden" name="_type" value="risk-information-notification" />
<input type="hidden" name="timestamp" value="2008-10-26T21:39:05.011Z" />
<input type="hidden" name="google-order-number" value="$ordernum" />
<input type="hidden" name="serial-number" value="$ordernum-00005-5" />
<input type="hidden" name="risk-information_billing-address_email" value="byrnereese@gmail.com" />
<input type="hidden" name="risk-information_billing-address_contact-name" value="Byrne Reese" />
<input type="hidden" name="risk-information_billing-address_address1" value="755 Walker Ave" />
<input type="hidden" name="risk-information_billing-address_country-code" value="US" />
<input type="hidden" name="risk-information_billing-address_city" value="oakland" />
<input type="hidden" name="risk-information_billing-address_region" value="CA" />
<input type="hidden" name="risk-information_billing-address_postal-code" value="94610" />
<input type="hidden" name="risk-information_ip-address" value="63.193.114.118" />
<input type="hidden" name="risk-information_avs-response" value="Y" />
<input type="hidden" name="risk-information_cvn-response" value="U" />
<input type="hidden" name="risk-information_eligible-for-protection" value="true" />
<input type="hidden" name="risk-information_partial-cc-number" value="1111" />
<input type="hidden" name="risk-information_buyer-account-age" value="747" />
<input type="submit" value="Risk Information Notification" />
</form>
END;

$post4 = <<<END
<form action="simulate-post.php" method="post">
<input type="hidden" name="_type" value="charge-amount-notification" />
<input type="hidden" name="timestamp" value="2008-10-26T21:39:05.172Z" />
<input type="hidden" name="google-order-number" value="$ordernum" />
<input type="hidden" name="latest-charge-amount_currency" value="USD" />
<input type="hidden" name="latest-charge-amount" value="750.0" />
<input type="hidden" name="total-charge-amount_currency" value="USD" />
<input type="hidden" name="total-charge-amount" value="750.0" />
<input type="hidden" name="serial-number" value="$ordernum-00010-2" />
<input type="submit" value="Charge Amount Notification" />
</form>
END;

$xml5 = <<<END
<?xml version="1.0" encoding="UTF-8"?>
<merchant-calculation-callback xmlns="http://checkout.google.com/schema/2">
  <shopping-cart>
    <merchant-private-data>
      <merchant-note>affiliate code 01234</merchant-note>
    </merchant-private-data>
    <cart-expiration>
      <good-until-date>2006-12-31T23:59:59.000Z</good-until-date>
    </cart-expiration>
    <items>
      <item>
        <quantity>1</quantity>
        <unit-price currency="USD">35.00</unit-price>
        <item-name>Dry Food Pack AA1453</item-name>
        <item-description>A pack of highly nutritious dried food for emergency</item-description>
        <tax-table-selector>food</tax-table-selector>
      </item>
      <item>
        <quantity>1</quantity>
        <unit-price currency="USD">178.00</unit-price>
        <item-name>MegaSound 2GB MP3 Player</item-name>
        <item-description>Portable MP3 player - stores 500 songs</item-description>
        <merchant-private-item-data>
          <item-note>Popular item: Check inventory and order more if needed</item-note>
        </merchant-private-item-data>
      </item>
    </items>
  </shopping-cart>
  <buyer-language>en_US</buyer-language>
  <buyer-id>294873009217523</buyer-id>
  <calculate>
    <addresses>
      <anonymous-address id="739030698069958">
        <country-code>US</country-code>
        <city>Ventura</city>
        <region>CA</region>
        <postal-code>93003</postal-code>
      </anonymous-address>
      <anonymous-address id="421273450774618">
        <country-code>US</country-code>
        <city>Mountain View</city>
        <region>CA</region>
        <postal-code>94043</postal-code>
      </anonymous-address>
    </addresses>
    <tax>true</tax>
    <shipping>
      <method name="SuperShip" />
      <method name="UPS Ground" />
    </shipping>
    <merchant-code-strings>
      <merchant-code-string code="GiftCert012345" />
      <merchant-code-string code="FirstVisitCoupon" />
    </merchant-code-strings>
  </calculate>
</merchant-calculation-callback>
END;

?>
<html>
  <head>
    <title>Google Checkout Simulator</title>
  </head>
  <body>
    <h1>Google Checkout Simulator</h1>
<?php
if ($_REQUEST['_type'] == 'new-order-notification') {
  print "    <p><b>Response sent to Google:</b></p>\n";
  print "    <pre>".htmlentities($server->handlePost())."</pre>";

} elseif ($_REQUEST['_type'] == 'order-state-change-notification') {
  print "    <p><b>Response sent to Google:</b></p>\n";
  print "    <pre>".htmlentities($server->handlePost())."</pre>";

} elseif ($_REQUEST['_type'] == 'risk-information-notification') {
  print "    <p><b>Response sent to Google:</b></p>\n";
  print "    <pre>".htmlentities($server->handlePost())."</pre>";

} elseif ($_REQUEST['_type'] == 'charge-amount-notification') {
  print "    <p><b>Response sent to Google:</b></p>\n";
  print "    <pre>".htmlentities($server->handlePost())."</pre>";

} elseif ($_REQUEST['step'] == 5) {
  print "    <h2>Sending charge-order-notification</h2>\n";
  eval('$server->sendChargeOrderNotification("250493131124632");');

} elseif ($_REQUEST['step'] == 6) {
  print "    <p><b>Response sent to Google:</b></p>\n";
  print "    <pre>".htmlentities($server->handle($xml5))."</pre>";

} else {
  print "    <h2>Start a Real Transaction</h2>\n";
  include("lib/GoogleCheckout.php");
  $cart = new GoogleShoppingCart();
  $item = new CartItem("Test Run","Software","1.00",1);
  $cart->addPrivateData("discountCode","BETA-TEST");
  $cart->addItem($item);
  $cart->acceptDiscounts = 1;
  $cart->callbackURL = CALLBACK_URL;
  print $cart->button->toHTML($cart);
}
?>
    <h2>Simulate a Notification</h2>
    <ul>
      <li><?php echo $post1; ?></li>
      <li><?php echo $post2; ?></li>
      <li><?php echo $post3; ?></li>
      <li><?php echo $post4; ?></li>
      <li><a href="?step=5">Send charge-order-notification</a></li>
      <li><a href="?step=6">Merchant Calculation Callback</a></li>
    </ul>
    <hr />
    <p>Software by <a href="http://www.majordojo.com/">majordojo</a></p>
  </body>
</html>